home *** CD-ROM | disk | FTP | other *** search
- /*
- * DialogUtilities.c
- *
- * (c) Apple Computer, Inc 1993
- */
-
- #include <Dialogs.h>
- #include <DiskInit.h>
- #include <Errors.h>
- #include <Events.h>
- #include <Menus.h>
- #include <OSUtils.h>
- #include <Packages.h>
- #include <Quickdraw.h>
- #include <Resources.h>
- #include <Script.h>
- #include <TextEdit.h>
- #include <ToolUtils.h>
- #include <Types.h>
- #include <Windows.h>
-
- #include "DialogUtilities.h"
-
- short DoModalEvent (ModalFilterProcPtr filterProc);
- void DoMouseDown (EventRecord *theEvent);
- void DoDiskEvent (EventRecord *theEvent);
- void DoApp4Event (EventRecord *theEvent);
- void DoUpdate (EventRecord *theEvent);
- void DoDrag (WindowPtr whichWindow, Point where);
-
- static Boolean gFiltered;
-
- /*
- * dialog event handling
- */
-
- void
- MovableDialog(ModalFilterProcPtr filterProc, short *itemHit)
- {
- *itemHit = 0;
- if (FrontWindow () != NULL)
- {
- gFiltered = false;
- while (!gFiltered)
- {
- *itemHit = DoModalEvent(filterProc);
- }
- }
- }
-
- pascal Boolean
- StandardFilter(DialogPtr whichDialog, EventRecord *event, short *itemHit)
- {
- #define returnKey 13
- #define enter 3
- #define esc 27
- #define pushButton (ctrlItem + btnCtrl)
-
- Boolean filtered;
- DialogPeek whichPeek;
- short defItem;
- char key;
- short itemType;
- Handle itemHandle;
- Rect itemRect;
- long finalTicks;
-
- filtered = false;
- whichPeek = (DialogPeek) whichDialog;
- defItem = whichPeek->aDefItem;
- switch ((*event).what)
- {
- case keyDown:
- case autoKey:
- key = (*event).message & charCodeMask;
- if ((key == returnKey) || (key == enter) || (key == esc)
- || ((key == '.') && (((*event).modifiers & cmdKey) != 0)))
- {
- if ((key == returnKey) || (key == enter))
- {
- *itemHit = defItem;
- }
- else
- {
- *itemHit = cancel;
- }
- GetDItem (whichDialog, *itemHit, &itemType, &itemHandle, &itemRect);
- if ((itemType & itemDisable) == 0)
- {
- if ((itemType & (255 - itemDisable)) == pushButton)
- {
- HiliteControl ((ControlHandle) itemHandle, inButton);
- Delay (8, &finalTicks);
- HiliteControl ((ControlHandle) itemHandle, 0);
- }
- filtered = true;
- }
- }
- else
- {
- if ((((*event).modifiers & cmdKey) != 0) &&
- ((key == 'x') || (key == 'c') || (key == 'v')))
- {
- *itemHit = (whichPeek)->editField + 1;
- switch (key) {
- case 'x':
- DlgCut (whichDialog);
- break;
- case 'c':
- DlgCopy (whichDialog);
- *itemHit = 0;
- break;
- case 'v':
- DlgPaste (whichDialog);
- break;
- }
- if (*itemHit > 0)
- {
- GetDItem (whichDialog, *itemHit, &itemType, &itemHandle, &itemRect);
- if ((itemType & itemDisable) == 0)
- {
- filtered = true;
- }
- }
- if (!filtered)
- {
- (*event).what = nullEvent;
- }
- }
- }
- break;
-
- case updateEvt:
- if ((WindowPtr) (*event).message == qd.thePort)
- {
- GetDItem (whichDialog, defItem, &itemType, &itemHandle, &itemRect);
- if ((itemType & (255 - itemDisable)) == pushButton)
- {
- OutlineDItem(whichDialog, defItem);
- }
- }
- else if (!IsDialogEvent (event))
- {
- DoUpdate(event);
- }
- break;
- }
-
- return (filtered);
- }
-
- static short
- DoModalEvent(ModalFilterProcPtr filterProc)
- {
- WindowPtr eventWindow;
- short itemHit;
- GrafPtr savePort;
- DialogPtr dialog;
- EventRecord theEvent;
- Boolean gotEvent;
-
- GetPort(&savePort);
-
- gFiltered = false;
- itemHit = 0;
- dialog = FrontWindow();
- gotEvent = WaitNextEvent (everyEvent, &theEvent, 0, NULL);
- if (gotEvent || (theEvent.what == nullEvent))
- {
- if (theEvent.what == app4Evt)
- {
- DoApp4Event (&theEvent);
- }
- if (IsDialogEvent (&theEvent))
- {
- if ((theEvent.what == activateEvt) || (theEvent.what == updateEvt))
- {
- eventWindow = (DialogPtr) theEvent.message;
- }
- else
- {
- eventWindow = FrontWindow ();
- }
- SetPort (eventWindow);
- if (filterProc != NULL)
- {
- gFiltered = (*filterProc) (dialog, &theEvent, &itemHit);
- }
- else
- {
- gFiltered = StandardFilter (dialog, &theEvent, &itemHit);
- }
- if (!gFiltered)
- {
- gFiltered = DialogSelect (&theEvent, &dialog, &itemHit);
- }
- }
- else
- {
- switch (theEvent.what)
- {
- case mouseDown:
- DoMouseDown(&theEvent);
- break;
-
- case mouseUp:
- case keyDown:
- case autoKey:
- break;
-
- case updateEvt:
- DoUpdate(&theEvent);
- break;
-
- case activateEvt:
- break;
-
- case diskEvt:
- DoDiskEvent(&theEvent);
- break;
- }
- }
- }
-
- SetPort(savePort);
-
- return itemHit;
- }
-
- static void
- DoMouseDown(EventRecord *theEvent)
- {
- long menuChoice;
- short menuID;
- short itemNr;
- WindowPtr whichWindow;
- short whichPart;
-
- whichPart = FindWindow (theEvent->where, &whichWindow);
- switch (whichPart)
- {
- case inMenuBar:
- menuChoice = MenuSelect (theEvent->where);
- menuID = HiWord (menuChoice);
- itemNr = LoWord (menuChoice);
- if ((menuID != 0) && (itemNr != 0))
- {
- SysBeep (1);
- }
- break;
-
- case inDrag:
- if (whichWindow == FrontWindow ())
- {
- DoDrag (whichWindow, theEvent->where);
- }
- else
- {
- SysBeep (1);
- }
- break;
-
- case inSysWindow:
- case inContent:
- case inDesk:
- case inGrow:
- case inGoAway:
- case inZoomIn:
- case inZoomOut:
- default:
- SysBeep (1);
- break;
- }
- }
-
- void
- DoDrag(WindowPtr whichWindow, Point where)
- {
- RgnHandle grayRgn;
- Rect limitRect;
-
- grayRgn = GetGrayRgn();
- limitRect = (**grayRgn).rgnBBox;
- DragWindow(whichWindow, where, &limitRect);
- }
-
- static void
- DoDiskEvent(EventRecord *theEvent)
- {
- #define dlgTop 75
- #define dlgLeft 100
-
- OSErr ignore;
- Point where;
-
- if (HiWord(theEvent->message) != noErr)
- {
- DILoad();
- SetPt(&where, dlgLeft, dlgTop);
- ignore = DIBadMount(where, theEvent->message);
- DIUnload();
- }
- }
-
- static void
- DoApp4Event(EventRecord */*theEvent*/)
- {
- ;
- }
-
- void
- DoUpdate(EventRecord *theEvent)
- {
- GrafPtr savePort;
- WindowPtr whichWindow;
-
- GetPort (&savePort);
- whichWindow = (WindowPtr) theEvent->message;
- SetPort (whichWindow);
- BeginUpdate (whichWindow);
- ;
- EndUpdate (whichWindow);
- SetPort (savePort);
- }
-
- /*
- * dialog item handling
- */
-
- void
- SetDUserItem(DialogPtr dialog, short item, ProcPtr drawProc)
- {
- Handle iHndl;
- Rect iRect;
- short iType;
-
- GetDItem(dialog, item, &iType, &iHndl, &iRect);
- SetDItem(dialog, item, iType, (Handle) drawProc, &iRect);
- }
-
- void
- EnableDItem(DialogPtr dialog, short item, Boolean enable)
- {
- short itemType;
- Handle itemHandle;
- Rect itemRect;
-
- GetDItem (dialog, item, &itemType, &itemHandle, &itemRect);
- if (enable) {
- itemType &= ~itemDisable;
- } else {
- itemType |= itemDisable;
- }
- SetDItem (dialog, item, itemType, itemHandle, &itemRect);
-
- if ((itemType & ctrlItem) != 0)
- {
- HiliteControl ((ControlHandle) itemHandle, (enable ? 0 : 255));
- }
- }
-
- void
- GetDText(DialogPtr dialog, short item, char *text)
- {
- short itemType;
- Handle itemHandle;
- Rect itemRect;
-
- GetDItem(dialog, item, &itemType, &itemHandle, &itemRect);
- if (itemHandle != nil) GetIText(itemHandle, text);
- }
-
- void
- SetDText(DialogPtr dialog, short item, char *text)
- {
- short itemType;
- Handle itemHandle;
- Rect itemRect;
- char oldText[256];
-
- GetDItem(dialog, item, &itemType, &itemHandle, &itemRect);
-
- itemType &= 0xFF7F;
-
- switch (itemType)
- {
- case ctrlItem + btnCtrl:
- case ctrlItem + chkCtrl:
- case ctrlItem + radCtrl:
- case ctrlItem + resCtrl:
- GetCTitle((ControlHandle) itemHandle, oldText);
- if (!EqualString(text, oldText, false, false))
- SetCTitle((ControlHandle) itemHandle, text);
- break;
-
- case statText:
- case editText:
- GetIText(itemHandle, oldText);
- if (!EqualString(text, oldText, false, false))
- SetIText(itemHandle, text);
- break;
- }
- }
-
- void
- SetDRadioButton(DialogPtr dialog, short theBtn, short firstBtn, short lastBtn)
- {
- short iType, i;
- Handle iHdl;
- Rect iRect;
-
- for (i = firstBtn; i <= lastBtn; i++)
- {
- GetDItem(dialog, i, &iType, &iHdl, &iRect);
- if (iHdl != nil) SetCtlValue((ControlHandle) iHdl, theBtn == i ? 1 : 0);
- }
- }
-
- short
- GetDRadioButton(DialogPtr dialog, short firstBtn, short lastBtn)
- {
- short iType, i;
- Handle iHdl;
- Rect iRect;
-
- for (i = firstBtn; i <= lastBtn; i++)
- {
- GetDItem(dialog, i, &iType, &iHdl, &iRect);
- if (iHdl != nil)
- if (GetCtlValue((ControlHandle) iHdl)) break;
- }
- return i;
- }
-
- void
- OutlineDItem(DialogPtr dialog, short item)
- {
- PenState savePen;
- Handle iHndl;
- Rect iRect;
- short iType;
- short curveFact;
-
- GetDItem(dialog, item, &iType, &iHndl, &iRect);
-
- GetPenState(&savePen);
- PenNormal();
- PenSize(3, 3);
- InsetRect(&iRect, -4, -4);
- curveFact = (iRect.bottom - iRect.top + 8) / 2;
- FrameRoundRect(&iRect, curveFact, curveFact);
- SetPenState(&savePen);
- }
-
- pascal void
- DrawItemLineGray(DialogPtr dialog, short item)
- {
- Handle item_handle;
- Rect item_rect;
- short item_type;
-
- GetDItem(dialog, item, &item_type, &item_handle, &item_rect);
-
- if ((item_rect.right - item_rect.left) > (item_rect.bottom - item_rect.top))
- {
- PenPat(&qd.gray);
- MoveTo(item_rect.left, item_rect.top);
- LineTo(item_rect.right - 1, item_rect.top);
- PenPat(&qd.black);
- }
- else
- {
- PenPat(&qd.gray);
- MoveTo(item_rect.left, item_rect.top);
- LineTo(item_rect.left, item_rect.bottom - 1);
- PenPat(&qd.black);
- }
- }
-
- pascal void
- DrawItemRect(DialogPtr dialog, short item)
- {
- Handle item_handle;
- Rect item_rect;
- short item_type;
-
- GetDItem(dialog, item, &item_type, &item_handle, &item_rect);
- FrameRect(&item_rect);
- }
-